package com.java.atm.Server;

import java.net.ServerSocket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Server
{
    public static int CURRENTUSERS = 0;
    private ServerSocket serverSocket;
    private Connection connection;

    public boolean connectDB()
    {
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            String dbURL = "jdbc:mysql://localhost:3306/java?user=root&password=jazz";
            connection = DriverManager.getConnection(dbURL);
            if(connection != null)
            {
                return true;
            }
            else
            {
                return false;
            }

        }catch(ClassNotFoundException e)
        {
            System.err.println("no driver!");
        }catch(SQLException e)
        {
            e.getErrorCode();
        }
        return true;

    }

    public void start()throws Exception
    {
        if( connectDB() )
        {
            System.out.println("数据库连接成功");
        }
        else
        {
            System.out.println("数据库连接失败,请检查是否设置正确");
            return;
        }


        serverSocket = new ServerSocket(8080);
        while(true)
        {
            ServerThread serverThread = new ServerThread(serverSocket.accept(), connection);
            Thread thread = new Thread(serverThread);
            thread.start();
        }
    }
}
